﻿using System;
using System.Web.Mvc;
using CharacterModel;
using Framework.Model;
using Ninject;

namespace MvcApplication1.Controllers
{
    public class CharacterInfoController : Controller
    {
        [Inject]
        public Repository Repository { get; set; }

        public ActionResult Edit(int characterInfoId)
        {
            var characterInfo = Repository.Get<CharacterInfo>(characterInfoId);
            //BUG: Very temporary work-around
            characterInfo.Portrait = Repository.Get<Portrait>(characterInfo.Portrait.Id);
            return View(characterInfo);
        }

        public ActionResult Create(int portraitId)
        {
            var characterInfo = new CharacterInfo{Portrait = Repository.Get<Portrait>(portraitId)};
            return View("Edit", characterInfo);
        }

        public ActionResult Save(CharacterInfo model)
        {
            try
            {
                Repository.Save(model);
            }
            catch (Exception e)
            {
                return Json(new {ErrorMessage = e.Message});
            }
            return Json(new {SavedId = model.Id});
        }
    }
}
